import sys
import re

"""Baby Names exercise

Define the extract_names() function below and change main()
to call it.

For writing regex, it's nice to include a copy of the target
text for inspiration.

Here's what the html looks like in the baby.html files:
...
<h3 align="center">Popularity in 1990</h3>
....
<tr align="right"><td>1</td><td>Michael</td><td>Jessica</td>
<tr align="right"><td>2</td><td>Christopher</td><td>Ashley</td>
<tr align="right"><td>3</td><td>Matthew</td><td>Brittany</td>
...

Suggested milestones for incremental development:
 -Extract the year and print it
 -Extract the names and rank numbers and just print them
 -Get the names data into a dict and print it
 -Build the [year, 'name rank', ... ] list and print it
 -Fix main() to use the extract_names list
"""


def test(filename):
  str = open(filename, encoding='utf-8'), features = 'html.parser'
  datas = str.text
  # print(type(data))
  datas = datas.split()
  datas = datas[34:]
  list1 = [filename.split(".")[0][-4:]]
  for data in datas:
    dig = re.sub("\D", "", data)
    data = data.replace(dig, "")
    # print(data)
    for i in range(1, len(data)):
      if data[i].isupper() and data[i - 1].islower():
        list1.append(data[:i] + " " + dig)
        list1.append(data[i:] + " " + dig)
        break
      # print(na)
  list1.sort()
  return list1


  return


def main():
  # This command-line parsing code is provided.
  # Make a list of command line arguments, omitting the [0] element
  # which is the script itself.
  args = sys.argv[1:]

  if not args:
    print 'usage: [--summaryfile] file [file ...]'
    sys.exit(1)

  # Notice the summary flag and remove it from args if it is present.
  summary = False
  if args[0] == '--summaryfile':
    summary = True
    del args[0]

    # +++your code here+++
    # For each filename, get the names, then either print the text output
    # or write it to a summary file
    if summary:
      for a_file in args:
        extract_names(a_file)
  
if __name__ == '__main__':
  main()
